#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")

DATA_PATH <- file.path(R_RESULTS_PATH, "HRCompare")


#'#
#'# Chargement des dependances
#'# 

#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))



#' hrStatsTracksFilter
load(file.path(DATA_PATH, "hrStatsTracksFilter.RData"))
#' hrStatsTracksMobile
load(file.path(DATA_PATH, "hrStatsTracksMobile.RData"))
#' hrStatsTracksStatic
load(file.path(DATA_PATH, "hrStatsTracksStatic.RData"))



hrStatsTracks <- rbindlist(list(hrStatsTracksFilter, hrStatsTracksMobile, hrStatsTracksStatic))
hrStatsTracks[, type := factor(type, levels=c("Raw", "LC32", "LC321",
            "DAF","SF", "TrStat", "TrMob"))]

hrStatsTracks95 <- hrStatsTracks[percent == 95]

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Statistics                                                   ##
#'#                                                                                              ##
#'#################################################################################################


HRmethod <- "kernel"
hrStatsTracksLM <- hrStatsTracks95[method == HRmethod]

#' Kernel 95


mserr <- hrStatsTracksLM[type %in% c("LC32", "TrStat", "TrMob")]
filt <- hrStatsTracksLM[type %in% c("Raw", "LC32", "LC321", "DAF", "SF")]


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Simulations erreur Mobile/Statique                           ##
#'#                                                                                              ##
#'#################################################################################################

mserr1 <- hrStatsTracksLM[type %in% c("LC32", "TrStat")]
mserr2 <- hrStatsTracksLM[type %in% c("LC32", "TrMob")]
mserr3 <- hrStatsTracksLM[type %in% c("TrStat", "TrMob")]

#'#
#'# Size Ratio
#'# 
lmMserrRatio1 <- lmer(sizeRatio.mean ~ type + (1|deployment), data=mserr1)
lmMserrRatio2 <- lmer(sizeRatio.mean ~ type + (1|deployment), data=mserr2)
lmMserrRatio3 <- lmer(sizeRatio.mean ~ type + (1|deployment), data=mserr3)
summary(lmMserrRatio1)
summary(lmMserrRatio2)
summary(lmMserrRatio3)

#'#
#'# Overlap Index
#'# 

lmMserrOI1 <- lmer(overlapIndex.mean ~ type + (1|deployment), data=mserr1)
lmMserrOI2 <- lmer(overlapIndex.mean ~ type + (1|deployment), data=mserr2)
lmMserrOI3 <- lmer(overlapIndex.mean ~ type + (1|deployment), data=mserr3)
summary(lmMserrOI1)
summary(lmMserrOI2)
summary(lmMserrOI3)

#'#
#'# Prop in Ref
#'# 

lmMserrPiR1 <- lmer(propInRef.mean ~ type + (1|deployment), data=mserr1)
lmMserrPiR2 <- lmer(propInRef.mean ~ type + (1|deployment), data=mserr2)
lmMserrPiR3 <- lmer(propInRef.mean ~ type + (1|deployment), data=mserr3)
summary(lmMserrPiR1)
summary(lmMserrPiR2)
summary(lmMserrPiR3)


#'#
#'# Home range size
#'# 

lmMserrHRS1 <- lmer(comparedArea.mean ~ type + (1|deployment), data=mserr1)
lmMserrHRS2 <- lmer(comparedArea.mean ~ type + (1|deployment), data=mserr2)
lmMserrHRS3 <- lmer(comparedArea.mean ~ type + (1|deployment), data=mserr3)
summary(lmMserrHRS1)
summary(lmMserrHRS2)
summary(lmMserrHRS3)



#'#################################################################################################
#'#                                                                                              ##
#'#                                 Simulations erreur Filtre                                    ##
#'#                                                                                              ##
#'#################################################################################################


#'#
#'# Home range size
#'# 

lmFiltHRS <- lmer(comparedArea.mean ~ type + (1|deployment), data=filt)
summary(lmFiltHRS)
difflsmeans(lmFiltHRS)


#'#
#'# Size Ratio
#'# 

lmFiltRatio <- lmer(sizeRatio.mean ~ type + (1|deployment), data=filt)
summary(lmFiltRatio)
difflsmeans(lmFiltRatio)


#'#
#'# Overlap Index
#'# 

lmFiltOI <- lmer(overlapIndex.mean ~ type + (1|deployment), data=filt)
summary(lmFiltOI)
difflsmeans(lmFiltOI)


#'#
#'# Prop in Ref
#'# 

lmFiltPiR <- lmer(propInRef.mean ~ type + (1|deployment), data=filt)
summary(lmFiltPiR)
lsmeans(lmFiltPiR)
difflsmeans(lmFiltPiR)


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Ratio kernel/MCP                                             ##
#'#                                                                                              ##
#'#################################################################################################

load(file.path(DATA_PATH, "hrStatsTracksFilter.RData"))
load(file.path(DATA_PATH, "hrStatsTracksStatic.RData"))
load(file.path(DATA_PATH, "hrStatsTracksMobile.RData"))
load(file.path(DATA_PATH, "hrStatsRadius.RData"))

r <- rbind(hrStatsTracksFilter, hrStatsTracksStatic, hrStatsTracksMobile, hrStatsRadius)
r <- r[!type == "Ref"]

rm <- r[method=="mcp" & percent == 95]
rk <- r[method=="kernel" & percent == 95]
setkey(rm, type, deployment)
setkey(rk, type, deployment)

rk1 <- rk[, list(sr=sizeRatio.mean, pir=propInRef.mean, size=(comparedArea.mean))]
rm1 <- rm[, list(sr=sizeRatio.mean, pir=propInRef.mean, size=(comparedArea.mean))]

rat <- rk1/rm1
rat[, lapply(.SD, function(x){return (list(m=mean(x), sd=(sd(x))))}), .SDcols=c("sr", "pir", "size")]
rk[, list(mean=mean(rat), sd=sd(rat)), by=stat]



raw <- hrStatsTracksFilter[type == "Raw" & percent == 95]
filters <- hrStatsTracksFilter[!type %in% c("Raw", "Ref") & percent == 95]
setkey(raw, type, method, deployment)
setkey(filters, type, method, deployment)

r <- filters[, (raw$sizeRatio.mean - sizeRatio.mean) / raw$sizeRatio.mean * 100, by=type]
r [, method := raw$method]
r[, list(mean = mean(V1), sd = sd(V1)), by = list(method, type)]




load(file.path(DATA_PATH, "hrGraphsTracksAll.RData"))
raw <- hrGraphsTracksAll[type == "Raw" & percent == 95]
filters <- hrGraphsTracksAll[!type %in% c("Raw", "Ref") & percent == 95]
setkey(raw, type, method, stat)
setkey(filters, type, method, stat)

r <- filters[, ((raw$mean - mean) / raw$mean) * 100, by=type]
r[, method := raw$method]
r[, stat := raw$stat]

r[stat=="sizeRatio"]
r[stat=="comparedArea"]
r[stat=="propInRef"]
